package tut.collection.dataStructure.compareableVsComparator.sortHashMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SortListOfHashMap {
	private static final String idKey = "ID";
	private static final String nameKey = "NAME";

	public static void main(String[] args) {
		SortListOfHashMap SortListOfHashMap = new SortListOfHashMap();

		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		list.add(SortListOfHashMap.makeMap("002", "name2"));
		list.add(SortListOfHashMap.makeMap("001", "name1"));

		SortListOfHashMap.printMap(list);
		Collections.sort(list, SortListOfHashMap.new comp());
		SortListOfHashMap.printMap(list);
	}

	private Map<String, String> makeMap(String id, String name) {
		Map<String, String> m1 = new HashMap<String, String>();
		m1.put(idKey, id);
		m1.put(nameKey, name);
		return m1;
	}

	private void printMap(List<Map<String, String>> list) {
		System.out.println("-------------------------");
		for (Map<String, String> map : list) {
			System.out.println(map.get(idKey));
			System.out.println(map.get(nameKey));
		}
		System.out.println("-------------------------");
	}

	/**
	 * ソート用のインナークラス
	 */
	class comp implements Comparator<Map<String, String>>{
		public int compare(Map<String, String> m1, Map<String, String> m2){
			String s1 = m1.get(idKey);
			String s2 = m2.get(idKey);
			return s1.compareTo(s2);
		}
	}
}
